﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SQLExpressDAL;
using System.Data.SqlClient;

public partial class Manageweb_Users_UpdateUserPassword : System.Web.UI.Page
{
    public string StrKey;
    String sql;
    int num, num1;

    protected override void Render(HtmlTextWriter writer)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        base.Render(htw);

        if (!hidMiYao.Value.Equals("1"))
        {
            writer.Write(sw.ToString().Replace("<body", "<body onload=javascript:openLayer('test','test_con');"));
        }
        else
        {
            writer.Write(sw);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        StrKey = CheckData();
        if (hidMiYao.Value.Equals("1"))
        {
            this.TABLE_ModPass.Visible = true;
        }   
    }
    protected void Btn_ModPass_Click(object sender, EventArgs e)
    {
        sql = "select count(*) from users where UserPassword='" + TB_OldPass.Text + "'";
        num = Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sql, null));
        if (num == 0)
        {
            Page.RegisterClientScriptBlock("", "<script language='javascript'> alert('原密码输入错误，请确认信息是否正确!');</script>");
        }
        else
        {
            SqlParameter[] paras ={ 
                new SqlParameter("@userid",SqlDbType.VarChar,100),
                new SqlParameter("@newPassword",SqlDbType.VarChar,100)
            };
            paras[0].Value = Session["userid"];
            paras[1].Value = TB_NewPass.Text;

            num1 = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "up_UpdatePassword", paras);
            if (num1 == 1)
            {
                Page.RegisterClientScriptBlock("", "<script language='javascript'> alert('密码更新成功!');</script>");
            }
            else
            {
                Page.RegisterClientScriptBlock("", "<script language='javascript'> alert('更新失败!');</script>");

            }
        }
    }


    //获取密钥Cdefine5
    public string CheckData()
    {
        SqlParameter[] paras ={ 
                new SqlParameter("@userid",SqlDbType.VarChar,30)
            };
        paras[0].Value = Session["userid"];

        StrKey = SqlHelper.FillDataSet(CommandType.StoredProcedure, "UP_GetUserKey", paras).Tables[0].Rows[0][1].ToString();
        return StrKey;
    }
}
